﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Portal.Web.Global;
using Portal.Web.Framework;
using Portal.Core;

namespace Portal.Gui.Web.WebParts.AssetTn
{
    public class SqlReaderServiceBll
    {
        public static string GetConnectionString(string SiteId)
        {
            SiteParam oSiteParam = Environments.CreateSiteParam(SiteId);
            string ConnectionString = oSiteParam.ConnectionString;
            return ConnectionString;
        }

        public static DataTable GetDataTable(string SiteId, string CommandText, ParamCls[] Params)
        {
            SqlConnection oSqlConnection = new SqlConnection(GetConnectionString(SiteId));
            oSqlConnection.Open();
            SqlCommand oSqlCommand = oSqlConnection.CreateCommand();
            oSqlCommand.CommandText = CommandText;
            if (Params != null)
            {
                for (int iIndex = 0; iIndex < Params.Length; iIndex++)
                {
                    oSqlCommand.Parameters.Add("@" + Params[iIndex].Key, SqlDbType.NVarChar).Value = Params[iIndex].Value;
                }
            }
            SqlDataAdapter sqlda = new SqlDataAdapter(oSqlCommand);
            DataTable dtResult = new DataTable();
            sqlda.Fill(dtResult);
            return dtResult;
        }

        public static DataTable GetDataTable(string SiteId, string ConnectionString, string CommandText, ParamCls[] Params)
        {
            SqlConnection oSqlConnection = new SqlConnection(ConnectionString);
            oSqlConnection.Open();
            SqlCommand oSqlCommand = oSqlConnection.CreateCommand();
            oSqlCommand.CommandText = CommandText;
            if (Params != null)
            {
                for (int iIndex = 0; iIndex < Params.Length; iIndex++)
                {
                    oSqlCommand.Parameters.Add("@" + Params[iIndex].Key, SqlDbType.NVarChar).Value = Params[iIndex].Value;
                }
            }
            SqlDataAdapter sqlda = new SqlDataAdapter(oSqlCommand);
            DataTable dtResult = new DataTable();
            sqlda.Fill(dtResult);
            return dtResult;
        }

        public static DataSet GetDataSet(string SiteId, string CommandText, ParamCls[] Params)
        {
            SqlConnection oSqlConnection = new SqlConnection(GetConnectionString(SiteId));
            oSqlConnection.Open();
            SqlCommand oSqlCommand = oSqlConnection.CreateCommand();
            oSqlCommand.CommandText = CommandText;
            if (Params != null)
            {
                for (int iIndex = 0; iIndex < Params.Length; iIndex++)
                {
                    oSqlCommand.Parameters.Add("@" + Params[iIndex].Key, SqlDbType.NVarChar).Value = Params[iIndex].Value;
                }
            }
            SqlDataAdapter sqlda = new SqlDataAdapter(oSqlCommand);
            DataSet dsResult = new DataSet();
            sqlda.Fill(dsResult);
            return dsResult;
        }

        public static DataSet GetDataSet(string SiteId, string ConnectionStrimg, string CommandText, ParamCls[] Params)
        {
            SqlConnection oSqlConnection = new SqlConnection(ConnectionStrimg);
            oSqlConnection.Open();
            SqlCommand oSqlCommand = oSqlConnection.CreateCommand();
            oSqlCommand.CommandText = CommandText;
            if (Params != null)
            {
                for (int iIndex = 0; iIndex < Params.Length; iIndex++)
                {
                    oSqlCommand.Parameters.Add("@" + Params[iIndex].Key, SqlDbType.NVarChar).Value = Params[iIndex].Value;
                }
            }
            SqlDataAdapter sqlda = new SqlDataAdapter(oSqlCommand);
            DataSet dsResult = new DataSet();
            sqlda.Fill(dsResult);
            return dsResult;
        }
    }
}
